/* eslint-disable no-undef */
import { UserManager } from 'oidc-client'
import Cookies from 'js-cookie'
// import { ACCESS_TOKEN, USER_NAME, ID_TOKEN } from '@/store/mutation-types'

const isProd = process.env.NODE_ENV === 'production'
const host = process.env.VUE_APP_IDS_HOST
const authsServer = process.env.VUE_APP_IDS_URL;

// const host = 'http://localhost:9528/#'
// const authsServer = "https://localhost:5001";

class ApplicationUserManager extends UserManager {
  constructor () {
    super({
        authority: authsServer,  // server 地址
        client_id: 'client_cb_manage',  // client id
        post_logout_redirect_uri: `${host}/`, // 退出登录
        redirect_uri: `${host}/callback#`,
        silent_redirect_uri: `${host}/silentRenew#`,
        accessTokenExpiringNotificationTime: 4,  // 超时
        silentRequestTimeout: 2000,  // 
        response_type: 'id_token token',
        scope: 'openid profile scope_cb_manage',
        filterProtocolClaims: true
   })
  }
  async login () {
    await this.signinRedirect()
    return this.getUser()
  }

  async logout () {
    console.log('调用 oidc-client 退出登陆')
    Cookies.remove('ACCESS_TYPE');
    Cookies.remove('ACCESS_TOKEN');
    Cookies.remove('USER_ID');
    Cookies.remove('SET_NAME');
    Cookies.remove('SET_HERDER_IMAGE');
    window.localStorage.clear();
    window.sessionStorage.clear();
    const idToken = Cookies.get('ID_TOKEN')
    return this.signoutRedirect({ 'id_token_hint': idToken })
  }
}

const applicationUserManager = new ApplicationUserManager()
export { applicationUserManager as default }
